<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\reminder\model;

class Config extends \app\BaseModel
{
    protected $name = "massage_aliyun_phone_config";
    public function getReminderAdminPhoneAttr($value, $data)
    {
        if(isset($value)) {
            return !empty($value) ? explode(",", $value) : "";
        }
    }
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function sendCalled($order, $phone = 0)
    {
        $p = new \app\reminder\info\PermissionReminder($order["uniacid"]);
        $auth = $p->pAuth();
        if(!$auth) {
            return true;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $order["uniacid"]]);
        if($config["reminder_status"] == 0) {
            return false;
        }
        $aliyun = new \longbingcore\wxcore\aliyun();
        $coach_model = new \app\massage\model\Coach();
        $record_model = new Record();
        $config_model = new \app\massage\model\ShortCodeConfig();
        $moor = new \longbingcore\wxcore\Moor($order["uniacid"]);
        $winner_model = new \longbingcore\wxcore\Winnerlook($order["uniacid"]);
        if(!empty($order["coach_id"])) {
            $phone = $coach_model->where(["id" => $order["coach_id"]])->value("mobile");
        }
        if($config["reminder_type"] == 1) {
            $res = $aliyun::main($order["uniacid"], $phone);
        } elseif($config["reminder_type"] == 2) {
            $res = $moor->webCall($phone);
        } else {
            $res = $winner_model->webCall($phone);
        }
        $insert = ["uniacid" => $order["uniacid"], "order_id" => $order["id"], "res" => json_encode($res)];
        $record_model->dataAdd($insert);
        $notice_admin = $config["notice_admin"] == 1 || $config["notice_admin"] == 0 && empty($order["admin_id"]) ? 1 : 0;
        if($notice_admin == 1 && !empty($config["reminder_admin_status"]) && !empty($config["reminder_admin_phone"]) && is_array($config["reminder_admin_phone"])) {
            foreach ($config["reminder_admin_phone"] as $value) {
                if($config["reminder_type"] == 1) {
                    $res = $aliyun::main($order["uniacid"], $value);
                } elseif($config["reminder_type"] == 2) {
                    $res = $moor->webCall($value);
                } else {
                    $res = $winner_model->webCall($value);
                }
                if(!empty($res["code"]) && ($res["code"] == "isv.BUSINESS_LIMIT_CONTROL" || $res["code"] == 402)) {
                    $config_model->sendSms($value, $order["uniacid"], $order["order_code"], 1);
                }
            }
        }
        if(!empty($order["admin_id"]) && !empty($config["notice_agent"]) && $config["notice_agent"] == 1) {
            $admin_model = new \app\massage\model\Admin();
            $phone = $admin_model->where(["id" => $order["admin_id"]])->value("phone");
            if(!empty($phone)) {
                if($config["reminder_type"] == 1) {
                    $res = $aliyun::main($order["uniacid"], $phone);
                } elseif($config["reminder_type"] == 2) {
                    $res = $moor->webCall($phone);
                } else {
                    $res = $winner_model->webCall($phone);
                }
                if(!empty($res["code"]) && ($res["code"] == "isv.BUSINESS_LIMIT_CONTROL" || $res["code"] == 402)) {
                    $config_model->sendSms($phone, $order["uniacid"], $order["order_code"], 1);
                }
            }
        }
        return $res;
    }
    public function sendPhoneNotice($order, $phone, $admin_id = 0)
    {
        $uniacid = $order["uniacid"];
        $p = new \app\reminder\info\PermissionReminder($uniacid);
        $auth = $p->pAuth();
        if(!$auth) {
            return true;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if($config["reminder_status"] == 0) {
            return false;
        }
        $aliyun = new \longbingcore\wxcore\aliyun();
        $short_config_model = new \app\massage\model\ShortCodeConfig();
        $res = $aliyun::main($uniacid, $phone);
        if($config["notice_agent"] == 1 && !empty($admin_id)) {
            $admin_model = new \app\massage\model\Admin();
            $phone = $admin_model->where(["id" => $order["admin_id"]])->value("phone");
            if(!empty($phone)) {
                $res = $aliyun::main($order["uniacid"], $phone);
                if($res["code"] == "isv.BUSINESS_LIMIT_CONTROL") {
                    $short_config_model->sendSms($phone, $order["uniacid"], $order["order_code"], 1);
                }
            }
        }
        return $res;
    }
    public function timingSendCalled($uniacid)
    {
        $p = new \app\reminder\info\PermissionReminder($uniacid);
        $auth = $p->pAuth();
        if(!$auth) {
            return true;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if($config["reminder_status"] == 0 || $config["reminder_timing"] == 0) {
            return false;
        }
        $order_model = new \app\massage\model\Order();
        $dis = ["uniacid" => $uniacid, "pay_type" => 2];
        $order = $order_model->where($dis)->where("pay_time", "<", time() - $config["reminder_timing"] * 60)->order("id desc")->limit(20)->select()->toArray();
        $record_model = new Record();
        $aliyun = new \longbingcore\wxcore\aliyun();
        $coach_model = new \app\massage\model\Coach();
        $refund_model = new \app\massage\model\RefundOrder();
        $winner_model = new \longbingcore\wxcore\Winnerlook($uniacid);
        if(!empty($order)) {
            $moor = new \longbingcore\wxcore\Moor($uniacid);
            foreach ($order as $value) {
                $refund_order = $refund_model->dataInfo(["order_id" => $value["id"], "status" => 1]);
                $dis = ["order_id" => $value["id"], "uniacid" => $value["uniacid"], "type" => 1];
                $find = $record_model->where($dis)->where("create_time", ">", time() - $config["reminder_timing"] * 60)->find();
                if(empty($find) && empty($refund_order)) {
                    $phone = $coach_model->where(["id" => $value["coach_id"]])->value("mobile");
                    if($config["reminder_type"] == 1) {
                        $res = $aliyun::main($uniacid, $phone);
                    } elseif($config["reminder_type"] == 2) {
                        $res = $moor->webCall($phone);
                    } else {
                        $res = $winner_model->webCall($phone);
                    }
                    $insert = ["uniacid" => $value["uniacid"], "order_id" => $value["id"], "res" => json_encode($res)];
                    $record_model->dataAdd($insert);
                }
            }
        }
        return true;
    }
    public function sendCalledPolice($coach)
    {
        $p = new \app\reminder\info\PermissionReminder($coach["uniacid"]);
        $auth = $p->pAuth();
        if(!$auth) {
            return true;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $coach["uniacid"]]);
        if($config["reminder_status"] == 0) {
            return false;
        }
        $help_model = new \app\massage\model\HelpConfig();
        $help_config = $help_model->dataInfo(["uniacid" => $coach["uniacid"]]);
        $aliyun = new \longbingcore\wxcore\aliyun();
        $moor = new \longbingcore\wxcore\Moor($coach["uniacid"]);
        $winner_model = new \longbingcore\wxcore\Winnerlook($coach["uniacid"]);
        if($help_config["reminder_admin_status"] == 1 && !empty($help_config["reminder_notice_phone"]) && (empty($coach["admin_id"]) || $help_config["reminder_notice_admin"] == 1)) {
            foreach ($help_config["reminder_notice_phone"] as $value) {
                if($config["reminder_type"] == 1) {
                    $res = $aliyun::main($coach["uniacid"], $value, 2);
                } elseif($config["reminder_type"] == 2) {
                    $res = $moor->webCall($value, 2);
                } else {
                    $res = $winner_model->webCall($value, 2);
                }
            }
        }
        if(!empty($coach["admin_id"]) && $help_config["help_reminder_agent_status"]) {
            $admin_model = new \app\massage\model\Admin();
            $phone = $admin_model->where(["id" => $coach["admin_id"]])->value("phone");
            if(!empty($phone)) {
                if($config["reminder_type"] == 1) {
                    $res = $aliyun::main($coach["uniacid"], $phone, 2);
                } elseif($config["reminder_type"] == 2) {
                    $res = $moor->webCall($phone, 2);
                } else {
                    $res = $winner_model->webCall($phone, 2);
                }
            }
        }
        return !empty($res) ? $res : true;
    }
    public function orderEndSendCalled($uniacid)
    {
        $p = new \app\reminder\info\PermissionReminder($uniacid);
        $auth = $p->pAuth();
        if(!$auth) {
            return true;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if($config["reminder_status"] == 0 || $config["order_end_status"] == 0) {
            return false;
        }
        $order_model = new \app\massage\model\Order();
        $dis = ["uniacid" => $uniacid, "pay_type" => 6, "is_add" => 0];
        $order = $order_model->where($dis)->order("id desc")->limit(20)->select()->toArray();
        $record_model = new Record();
        $aliyun = new \longbingcore\wxcore\aliyun();
        $coach_model = new \app\massage\model\Coach();
        $winner_model = new \longbingcore\wxcore\Winnerlook($uniacid);
        $moor = new \longbingcore\wxcore\Moor($uniacid);
        if(!empty($order)) {
            foreach ($order as $value) {
                $have_service_time = time() - $value["start_service_time"];
                $have_add_time_long = $order_model->where(["add_pid" => $value["id"], "pay_type" => 7])->sum("true_time_long");
                $add_time_long = $order_model->where(["add_pid" => $value["id"]])->where("pay_type", "in", [3, 4, 5, 6])->sum("true_time_long");
                $total_time = ($add_time_long + $value["true_time_long"]) * 60;
                $have_service_time = $have_service_time - $have_add_time_long * 60;
                if(300 < $total_time - $have_service_time) {
                } else {
                    $dis = ["order_id" => $value["id"], "uniacid" => $value["uniacid"], "type" => 2];
                    $find = $record_model->where($dis)->find();
                    if(empty($find)) {
                        $phone = $coach_model->where(["id" => $value["coach_id"]])->value("mobile");
                        if($config["reminder_type"] == 1) {
                            $res = $aliyun::main($uniacid, $phone, 3);
                        } elseif($config["reminder_type"] == 2) {
                            $res = $moor->webCall($phone, 3);
                        } else {
                            $res = $winner_model->webCall($phone, 3);
                        }
                        $insert = ["uniacid" => $value["uniacid"], "order_id" => $value["id"], "res" => json_encode($res), "type" => 2];
                        $record_model->dataAdd($insert);
                    }
                }
            }
        }
        return true;
    }
}

?>